<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface
namespace app\admin\controller;

use app\common;
use think\Controller;
use think\Session;
use think\Request;
use think\File;
class Cash extends common
{
    //列表
    public function index()
    {
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
        //搜索条件

        if (strlen($id)) {
            $where = "id='{$id}'";
        }
        //搜索条件
        if ($keyword){
            $where = "title LIKE '%{$keyword}%' OR writer LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%'";
        }

        //查询总数据
        $maxpages = Db('cash')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $list = db('cash')->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $list->render();

        //提现方式
        $banktype_con = array(
            "0" =>"银行卡",
            "1" =>"支付宝",
        );
        //提现状态
        $status_con =array(
                "0" => "等待受理",
                "1" => "提现成功",
                "2" => "提现失败",
            );

        // 模板变量赋值
        $this->assign("id",$id);
        $this->assign("keyword",$keyword);
        $this->assign('list', $list);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        $this->assign('type', $banktype_con);
        $this->assign('tixian', $status_con);
        $this->assign('empty', '<span class="empty">没有数据</span>');

        // 渲染模板输出
        return $this->fetch('cash/index');
    }

    //加载受理页面
    public function shouli(){
        $id = $_GET['id'];
        $user_id = $_GET['user_id'];
        $cash = db("cash")->where("id","=","{$id}")->find();
        $this->assign("info",$cash);
        $this->assign('loginname', $this->login());
        return $this->fetch('cash/shouli');
    }

    //提现受理
    public function do_shouli($id,$user_id){
//        var_dump($_POST);exit;
        $status = isset($_POST['status'])? $_POST['status'] : die("受理结果不能为空");
        $reason = $_POST['reason'];
        $time =time();

        $data=array();
        $data['status'] = $status;
        $data['end_time'] = $time;
        $data['shouli_type'] =1;
        $data['reason'] = $reason;
//        var_dump($data);exit;

        $cash = db("cash")
            ->where("id","=","{$id}")
            ->where("user_id","=","{$user_id}")
            ->update($data);
        if($cash){
            $sql = db("cash")
                ->where("id","=","{$id}")
                ->where("user_id","=","{$user_id}")
                ->find();
            $amount = $sql['amount'];
//            echo $amount;
//            var_dump($sql);exit;
            if($sql){
                if($sql['id'] == $id && $sql['status']==1){
                    //当提现成功时，冻结资金（提现资金）扣掉
                    $member = db("member")
                        ->where("id","=","{$user_id}")
                        ->field("un_integral,use_integral")
                        ->find();
//                                var_dump($member);exit;
                    $un_integral = 0;
                    $un_integral= $member['un_integral']-$amount;
                    $da=array();
                    $da['un_integral'] = $un_integral;
                    $update = db("member")
                        ->where("id","=","{$user_id}")
                        ->update($da);

                    //提现成功时，将申请的表单添加到积分流水表里
                        if($update){
                            $record=array();
                            $record['user_id']=$user_id;
                            $record['username']=$_POST['username'];
                            $record['jifen']=$_POST['amount'];
                            $record['bank']="--";
                            $record['balance']=$member['use_integral'];
                            $record['reason']=$reason;
                            $record['note']=$id;
                            $record['type']=1;
                            $record['add_time']=time();

                            $rec= db('record')
                                ->insertGetId($record);
                            if($rec < 0){
                                $this->error("添加积分流水表失败");
                            }
                        }else{
                            $this->error("提现成功，冻结资金扣除失败");
                        }

                }else if($sql['id'] == $id && $sql['status']==2){
                    //当提现失败时，冻结资金（提现资金）返回给用户
                    $member = db("member")
                        ->where("id","=","{$user_id}")
                        ->field("un_integral,use_integral")
                        ->find();
                    $use_integral = 0;
                    $use_integral = $member['use_integral']+$amount;
                    $un_integral = 0;
                    $un_integral= $member['un_integral']-$amount;

                    $da=array();
                    $da['un_integral'] = $un_integral;
                    $da['use_integral'] = $use_integral;
                    $update = db("member")
                        ->where("id","=","{$user_id}")
                        ->update($da);

                    //当申请失败时将申请的表单添加到积分流水表内
                    if($update){
                        $record=array();
                        $record['user_id']=$user_id;
                        $record['username']=$_POST['username'];
                        $record['jifen']=$_POST['amount'];
                        $record['bank']="--";
                        $record['balance']=$use_integral;
                        $record['reason']=$reason;
                        $record['note']=$id;
                        $record['type']=0;
                        $record['add_time']=time();
                        $rec = db('record')
                            ->insertGetId($record);
                        if($rec < 0){
                            $this->error("添加积分流水表失败");
                            }
                    }else{
                        $this->error("提现失败时，用户的冻结资金返回失败");
                    }

                }else{
                    $this->error("冻结资金操作失败");
                }
            }else{
                $this->error("查找提现订单失败");
            }

            $this->success("受理成功","member/index");
        }else{
            $this->error("受理失败");
        }
    }


    //加载查看受理后的详情
    public function chakan(){
        $id = $_GET['id'];
        $user_id = $_GET['user_id'];
//        echo $id,$user_id;exit;
        $sql = db("cash")->where("id","=","{$id}")->where("user_id","=","{$user_id}")->find();
//        var_dump($sql);exit;
        $this->assign("info",$sql);
        $this->assign('loginname', $this->login());
        if($sql){
//            var_dump($sql);exit;
            return $this->fetch("cash/look");
        }else{
            $this->error("数据没找到");
        }
    }

}